<?php

namespace app\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class User extends Model
{
    const IS_DELETE_NO = 0; // 未删除

    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    /**
     * @Description 判断用户名是否存在
     * @param string $name
     * @param int $userId
     * @return mixed|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @time 2023/08/28 16:31:08
     * @author: zj
     */
    public function checkNameIsExist(string $name,int $userId = 0)
    {
        $query = $this->field("id")
            ->where("name",$name)
            ->where("delete_time",self::IS_DELETE_NO);
        if ($userId) {
            $query->where('id','<>',$userId);
        }
        $info = $query->find();
        if (!$info) {
            return null;
        }
        return $info['id'];
    }

    /**
     * @Description 判断手机号是否存在
     * @author: zj
     * @param string $phone
     * @time 2023/08/28 16:32:18
     * @return mixed|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkPhoneIsExist(string $phone,int $userId = 0)
    {
        $query = $this->field("id")
            ->where("phone",$phone)
            ->where("delete_time",self::IS_DELETE_NO);
        if ($userId) {
            $query->where('id','<>',$userId);
        }
        $info = $query->find();
        if (!$info) {
            return null;
        }
        return $info['id'];
    }
}